package controllers;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

import play.data.validation.Required;
import models.Device;
import models.Operator;
import models.Oplog;
import models.Dealer;
import models.Shop;
import models.Standard;
import services.AuthorityService;
import services.DeviceService;
import services.HraStandardService;
import services.DealerService;
import services.ShopService;
import utils.BizConstants;
import utils.ExcelUtil;
import utils.FileConfig;
import utils.IdCardUtil;
import utils.Validate;
import vos.DealerVo;
import vos.DeviceVo;
import vos.HraStandardSearchVo;
import vos.ReturnCode;
import vos.ReturnListVo;
import vos.DealerSearchVo;
import vos.ShopSearchVo;

public class DealerController extends BaseController {
	/**
	 * 经销商列表页面
	 *
	 * @Title: dealerList 
	 * @return: void
	 */
	public static void dealerList(String menuid){
		Operator oper=getLoginUser();
		List<String> auths=AuthorityService.getAuthorityByOperAndMenu(menuid,oper.operatorid);
		for(String auth : auths){
			renderArgs.put(auth, auth);
		}
		render();
	}
	/**
	 * 获取经销商列表
	 *
	 * @Title: getDealer 
	 * @return: void
	 */
	public static void getDealer(int page,int rows, String sort,String order,
			String resperson,String dealerid,String dealername,
			Boolean isflag,String inviteid,String invitename){
		if(page==0){
			page=1;
		}
		if(rows==0){
			rows=BizConstants.pagesize;
		}
		DealerSearchVo vo =new DealerSearchVo(page,rows,order,sort,
				resperson,dealerid,dealername,isflag,inviteid,invitename);
		long total=DealerService.countDealer(vo);
		ReturnListVo listVo=new ReturnListVo();
		listVo.setTotal(total);
		List<DealerVo> lists=DealerService.listDealer(vo);
		listVo.setRows(lists);
		renderJSON(listVo);
	}
	/**
	 * 批量启用
	 *
	 * @Title: betchEnable 
	 * @return: void
	 */
	public static void betchEnable(String ids){
		if(!checkAuthority("DEALER_ENABLE")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		Operator oper=getLoginUser();
		if(StringUtils.isEmpty(ids)){
			renderFailedJson(ReturnCode.ErrorCode,"未选中记录");
		}
		boolean issuccess=DealerService.betchEnable(ids,oper.operatorid);
		if(issuccess){
			Oplog oplog=new Oplog(oper.operatorid,"批量启用经销商"+ids,"经销商管理");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode,"启用成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode,"启用失败");
		}
	}
	/**
	 * 批量停用
	 *
	 * @Title: betchEnable 
	 * @return: void
	 */
	public static void betchDisable(String ids){
		if(!checkAuthority("DEALER_DISABLE")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		Operator oper=getLoginUser();
		if(StringUtils.isEmpty(ids)){
			renderFailedJson(ReturnCode.ErrorCode,"未选中记录");
		}
		boolean issuccess=DealerService.betchDisable(ids,oper.operatorid);
		if(issuccess){
			Oplog oplog=new Oplog(oper.operatorid,"批量停用经销商"+ids,"经销商管理");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode,"停用成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode,"停用失败");
		}
	}
	/**
	 * 批量删除
	 *
	 * @Title: betchEnable 
	 * @return: void
	 */
	public static void betchDel(String ids){
		if(!checkAuthority("DEALER_BETCHDEL")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		if(StringUtils.isEmpty(ids)){
			renderFailedJson(ReturnCode.ErrorCode,"未选中记录");
		}
		String[] id=ids.split(",");
		List<String> listid=new ArrayList<String>();
		Collections.addAll(listid, id);
		List<String> shops = ShopService.selectByDealers(listid);
		if(shops!=null&&shops.size()>0){
			renderFailedJson(ReturnCode.ErrorCode,"经销商下有门店存在");
		}
		boolean issuccess=DealerService.betchDel(listid);
		if(issuccess){
			Operator oper=getLoginUser();
			Oplog oplog=new Oplog(oper.operatorid,"批量删除经销商"+ids,"经销商管理");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode,"删除成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode,"删除失败");
		}
	}
	/**
	 * 删除记录
	 *
	 * @Title: delDealer 
	 * @param id 经销商id
	 * @return: void
	 */
	public static void delDealer(String id){
		if(!checkAuthority("DEALER_DELETE")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		if(StringUtils.isEmpty(id)){
			renderFailedJson(ReturnCode.ErrorCode,"选中记录不能为空");
		}
		List<String> shop=ShopService.selectByDealer(id);
		if(shop!=null&&shop.size()>0){
			renderFailedJson(ReturnCode.ErrorCode,"经销商下有门店存在");
		}
		boolean issuccess=DealerService.delDealer(id);
		if(issuccess){
			Operator oper=getLoginUser();
			Oplog oplog=new Oplog(oper.operatorid,"删除经销商"+id,"经销商管理");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode,"删除成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode,"删除失败");
		}
	}
	/**
	 * 添加经销商
	 *
	 * @Title: addDealer 
	 * @return: void
	 */
	public static void dealerAdd(){
		render();
	}
	/**
	 * 复制经销商
	 *
	 * @Title: addDealer 
	 * @return: void
	 */
	public static void dealerCopy(String id){
		DealerVo oldDealer=DealerService.getById(id);
		renderArgs.put("dealer", oldDealer);
		render("DealerController/dealerAdd.html");
	}
	/**
	 * 验证经销商编号是否存在
	 *
	 * @Title: checkCode 
	 * @param dealerid 经销商id
	 * @return: void
	 */
	public static void checkCode(String value){
//		if(!Validate.isEnglishNum(value)){
//			renderFailedJson(ReturnCode.ErrorCode, "经销商编号格式不正确");
//		}
		if(StringUtils.isEmpty(value)){
			renderFailedJson(ReturnCode.ErrorCode, "编号不能为空");
		}
		boolean isexist=DealerService.checkCode(value);
		if(isexist){
			renderFailedJson(ReturnCode.ErrorCode, "编号已存在");
		}
		else{
			renderFailedJson(ReturnCode.SuccessCode, "该编号可以使用");
		}
	}
	/**
	 * 保存经销商信息
	 *
	 * @Title: saveDealer 
	 * @param dealer 经销商对象
	 * @return: void
	 */
	public static void saveDealer(Dealer dealer){
		if(!checkAuthority("DEALER_ADD")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		Operator oper=getLoginUser();
		if(dealer==null){
			renderFailedJson(ReturnCode.ErrorCode, "经销商不能为空");
		}
		if(StringUtils.isEmpty(dealer.dealerid)){
			renderFailedJson(ReturnCode.ErrorCode, "编号不能为空");
		}
		if(dealer.remark.length()>255){
			renderFailedJson(ReturnCode.ErrorCode, "备注长度不能大于255");
		}
		if(dealer.joindate==null){
			renderFailedJson(ReturnCode.ErrorCode, "加盟日期不能为空");
		}
		DealerVo oldDealer=DealerService.getById(dealer.dealerid);
		if(oldDealer!=null){
			renderFailedJson(ReturnCode.ErrorCode, "经销商已存在");
		}
		if(StringUtils.isNotEmpty(dealer.inviteid)){
			DealerVo invite=DealerService.getById(dealer.inviteid);
			if(invite==null){
				renderFailedJson(ReturnCode.ErrorCode, "邀请商不存在");
			}
		}
//		if(!Validate.isEnglishNum(dealer.dealerid)){
//			renderFailedJson(ReturnCode.ErrorCode, "经销商编号格式不正确");
//		}
		//验证手机号
		if(!Validate.isMobile(dealer.resmobile)){
			renderFailedJson(ReturnCode.ErrorCode, "手机格式不正确");
		}
		dealer.createname=oper.operatorid;
		boolean isSuccess=DealerService.saveDealer(dealer);
		if(isSuccess){
			Oplog oplog=new Oplog(oper.operatorid,"添加经销商"+dealer.dealerid,"经销商管理");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode, "保存成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode, "保存失败");
		}
	}
	/**
	 * 编辑经销商
	 *
	 * @Title: dealerEdit 
	 * @return: void
	 */
	public static void dealerEdit(String id){
		DealerVo oldDealer=DealerService.getById(id);
		renderArgs.put("dealer", oldDealer);
		render();
	}
	/**
	 * 保存经销商信息
	 *
	 * @Title: saveDealer 
	 * @param dealer 经销商对象
	 * @return: void
	 */
	public static void updateDealer(Dealer dealer){
		if(!checkAuthority("DEALER_EDIT")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		Operator oper=getLoginUser();
		if(dealer==null){
			renderFailedJson(ReturnCode.ErrorCode, "经销商不能为空");
		}
		if(StringUtils.isEmpty(dealer.dealerid)){
			renderFailedJson(ReturnCode.ErrorCode, "编号不能为空");
		}
		if(dealer.remark.length()>255){
			renderFailedJson(ReturnCode.ErrorCode, "备注长度不能大于255");
		}
		if(!IdCardUtil.verify(dealer.idcard)){
			renderFailedJson(ReturnCode.ErrorCode, "身份证号格式不正确");
		}
		if(dealer.joindate==null){
			renderFailedJson(ReturnCode.ErrorCode, "加盟日期不能为空");
		}
		DealerVo oldDealer=DealerService.getById(dealer.dealerid);
		if(oldDealer==null){
			renderFailedJson(ReturnCode.ErrorCode, "经销商不存在");
		}
		dealer.modifyname=oper.operatorid;
		boolean isSuccess=DealerService.updateDealer(dealer);
		if(isSuccess){
			Oplog oplog=new Oplog(oper.operatorid,"修改经销商"+dealer.dealerid,"经销商管理");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode, "保存成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode, "保存失败");
		}
	}
	/**
	 * 查看经销商
	 *
	 * @Title: dealerEdit 
	 * @return: void
	 */
	public static void dealerView(String id){
		DealerVo Dealer=DealerService.getById(id);
		renderArgs.put("dealer", Dealer);
		render();
	}
	/**
	 * 根据经销商id获取门店
	 *
	 * @Title: getShop 
	 * @param dealerid
	 * @return: void
	 */
	public static void getShop(int page,int rows, String sort,String order,String dealerid){
		if(StringUtils.isEmpty(dealerid)){
			renderJSON(null);
		}
		if(page==0){
			page=1;
		}
		if(rows==0){
			rows=BizConstants.pagesize;
		}
		ShopSearchVo vo =new ShopSearchVo(page,rows,order,sort,dealerid);
		long total=ShopService.countShopByDealer(vo);
		ReturnListVo listVo=new ReturnListVo();
		List<Shop> lists=ShopService.getShopByDealer(vo);
		listVo.setTotal(total);
		listVo.setRows(lists);
		renderJSON(listVo);
	}
	/**
	 * 获取combobox内容
	 *
	 * @Title: getModel 
	 * @return: void
	 */
	public static void getCombobox(){
		List<Dealer> shops=DealerService.getCombobox();
		List<Map<String,String>> combos=new ArrayList<Map<String,String>>();
		for(Dealer dealer : shops){
			Map<String,String> map=new HashMap<String,String>();
			map.put("id",dealer.dealerid);
			map.put("text", dealer.dealername);
			combos.add(map);
		}
		renderJSON(combos);
	}
	/**
	 * 导出为excel
	 *
	 * @Title: exportXls 
	 * @param dealerid
	 * @param dealername
	 * @param reesperson
	 * @return: void
	 */
	public static void exportXls(String dealerid, String dealername,
			String resperson,Boolean isflag,String inviteid,String invitename){
		if(!checkAuthority("DEALER_EXPORT")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		DealerSearchVo vo=new DealerSearchVo(dealerid,dealername,
				resperson,isflag,inviteid,invitename);
		List<DealerVo> lists=DealerService.exportXls(vo);
		long y=System.currentTimeMillis();
		String fileName = "经销商信息"+y+".csv";
        response.contentType = "application/x-download";
        Operator oper=getLoginUser();
        Oplog oplog=new Oplog(oper.operatorid,"导出经销商信息","经销商管理");
		oplog.save(oplog);
        renderBinary(ExcelUtil.buildExportFile(FileConfig.dealerXlsTemplate, lists), fileName);
	}
}
